package com.xzjie.core.shiro.session;

import java.io.Serializable;
import java.util.Collection;

import org.apache.shiro.session.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.xzjie.core.jedis.JedisManager;
import com.xzjie.utils.SerializeUtils;


public class JedisShiroSessionRepository implements ShiroSessionRepository {
	private static final Logger logger = LoggerFactory.getLogger(JedisShiroSessionRepository.class);
	private static final String REDIS_SHIRO_SESSION = "shiro-session:";
    private static final int SESSION_VAL_TIME_SPAN = 18000;
    //private static final int DB_INDEX = 0;

    private JedisManager jedisManager;

    @Override
    public void saveSession(Session session) {
        if (session == null || session.getId() == null)
            throw new NullPointerException("session is empty");
        try {
            byte[] key = SerializeUtils.serialize(buildRedisSessionKey(session.getId()));
            byte[] value = SerializeUtils.serialize(session);
            long sessionTimeOut = session.getTimeout() / 1000;
            Long expireTime = sessionTimeOut + SESSION_VAL_TIME_SPAN + (5 * 60);
            //getJedisManager().saveValueByKey(DB_INDEX, key, value, expireTime.intValue());
            getJedisManager().saveValueByKey( key, value, expireTime.intValue());
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("save session error");
        }
    }

    @Override
    public void deleteSession(Serializable id) {
        if (id == null) {
            throw new NullPointerException("session id is empty");
        }
        try {
            // getJedisManager().deleteByKey(DB_INDEX, SerializeUtils.serialize(buildRedisSessionKey(id)));
        	getJedisManager().deleteByKey( SerializeUtils.serialize(buildRedisSessionKey(id)));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("delete session error");
        }
    }

    @Override
    public Session getSession(Serializable id) {
        if (id == null)
            throw new NullPointerException("session id is empty");
        Session session = null;
        try {
//            byte[] value = getJedisManager().getValueByKey(DB_INDEX, SerializeUtils.serialize(buildRedisSessionKey(id)));
        	byte[] value = getJedisManager().getValueByKey( SerializeUtils.serialize(buildRedisSessionKey(id)));
        	session = SerializeUtils.deserialize(value, Session.class);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("get session error");
        }
        return session;
    }

    @Override
    public Collection<Session> getAllSessions() {
        //TODO
        System.out.println("get all sessions");
        return null;
    }

    private String buildRedisSessionKey(Serializable sessionId) {
        return REDIS_SHIRO_SESSION + sessionId;
    }

    public JedisManager getJedisManager() {
        return jedisManager;
    }

    public void setJedisManager(JedisManager jedisManager) {
        this.jedisManager = jedisManager;
    }
}
